<?php


namespace App\Http\Server;


use App\Models\Customer;
use App\Models\Goods;
use App\Models\RetailOrder;
use Illuminate\Support\Facades\DB;

class RetailRoderServer
{

    public function addRetailOrderServer($id,$customer_id,$goods_id,$pay_type,$num)
    {
        DB::transaction();
        if(isset($id) && !empty($id)){
            $model = RetailOrder::find($id);
        }else{
            $model = new RetailOrder();
        }
        $customer = Customer::find($customer_id);
        $goods = Goods::find($goods_id);
        $model->goods_id = $goods_id;
        $model->customer_id = $customer_id;
        $model->manufacturer_id = $goods->manufacturer_id;
        $model->pay_type = $pay_type;
        $model->goods_price = $goods->sales_price;
        $model->order_price = $goods->sales_price * $num;
        $model->wheat = $goods->wheat * $num;
        $model->num = $num;
        $model->order_time = date("Y-m-d H:i:s",time());
        $model->save();
        return ['status'=>200,'data'=>$model,'msg'=>'success'];
    }

    public function getRetailOrderListServer($params,$per_page)
    {
        // 执行查询
        $data = RetailOrder::Search($params)->with(['goods','manufacturer','customer'])->paginate($per_page)->toArray();
        return ['status'=>200,'data'=>$data['data'],'msg'=>'success'];
    }

    public function getRetailOrderInfoServer($id)
    {
        $info = RetailOrder::where('id',$id)->with(['goods','customer','manufacturer'])->first();
        return ['status'=>200,'data'=>$info,'msg'=>'success'];
    }

}
